PICKLED_CTX := ctx.pkl
BACKUP ?= $(shell bin/date "+%Y-%m-%d-%H-%M")

SHELL = /bin/bash
.SHELLFLAGS = -o pipefail -c

.PHONY: all clean backup recover clean-backup
all: $(PICKLED_CTX)

clean:
	rm -rf rtl syn vpr *.log *.pkl

backup:
	if [[ -d backup-$(BACKUP) ]]; then echo "Backup $(BACKUP) already exists"; exit 1; fi
	mkdir backup-$(BACKUP)
	cp $(PICKLED_CTX) backup-$(BACKUP)
	cp -r rtl backup-$(BACKUP)
	cp -r syn backup-$(BACKUP)
	cp -r vpr backup-$(BACKUP)

recover:
	if [[ ! -d backup-$(BACKUP) ]]; then echo "Backup $(BACKUP) does not exist"; exit 1; fi
	rm -rf $(PICKLED_CTX) && cp backup-$(BACKUP)/$(PICKLED_CTX) .
	rm -rf rtl && cp -r backup-$(BACKUP)/rtl .
	rm -rf syn && cp -r backup-$(BACKUP)/syn .
	rm -rf vpr && cp -r backup-$(BACKUP)/vpr .

clean-backup:
	rm -rf backup-*

$(PICKLED_CTX): build.py
	python -O $< $@ | tee build.log
